import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/common/su_dialog.dart';
import 'package:love_app/common/su_dio.dart';
import 'package:love_app/config/app_color.dart';
import 'package:love_app/config/path_url.dart';
import 'package:love_app/config/prefs.dart';
import 'package:love_app/enum/bind_lover.dart';
import 'package:love_app/enum/login_status.dart';
import 'package:love_app/enum/responst_status.dart';
import 'package:love_app/enum/sex.dart';
import 'package:love_app/model/login_model.dart';
import 'package:love_app/model/response_model.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/model/user_info_model.dart';
import 'package:love_app/pages/app_page.dart';
import 'package:love_app/service/base_api.dart';
import 'package:love_app/service/form_token_api.dart';
import 'package:love_app/service/user_info_api.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

// 登录接口
class LoginApi extends BaseApi {
  BuildContext context;

  LoginApi({this.context});

  // 登录接口
  login(String phone, String password) async {
    Map postData = {
      'phone': phone,
      'password': password,
    };
    String path = PathUrl.login;
    _startLoading(); // 开启loading
    ResponseModel responseModel = await SuDio().postRequest(path, data: postData);
    if (responseModel.status == ResponseStatusType.success) {
      closeLoading(context);
      LoginModel loginModel = LoginModel.fromJson(responseModel.toJsonModel);
      _setToekn(loginModel.data.token);
      bool re =  await _initShareModel(loginModel.data.token); // 初始化全局共享变量
      if(re == true){
        Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (_) => QuitLogin()), (Route router) => false);
      }
    }else{
      closeLoading(context);
    }
  }

  // 把token存储到本地
  Future<void> _setToekn(String token) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString(Prefs.token, token);
  }

  // 开启等待圆圈
  void _startLoading() {
    TextStyle textStyle = Su().textStyle(ThemeColor, 30);
    SuShowLoading(context,
            title: '登录中',
            loadColor: ThemeColor,
            textStyle: textStyle,
            isCricle: false)
        .buildLoading();
  }

  // // 在登录的时候获取UserModel并初始化
  // void _initUserModel(String token)async{
  //   Map<String, dynamic> model =  await UserInfoApi().getUserInfoData(token);
  //   UserInfoModel userInfoModel = Provider.of<UserInfoModel>(context);
  //   userInfoModel.initFrom(model);
  // }

    // 在登录的时候获取UserModel并初始化
  Future<bool> _initShareModel(String token)async{
    UserInfoModel model =  await UserInfoApi().getUserInfo(token);
    ShareProvider shareProvider = Provider.of<ShareProvider>(context);
    Sex userSex = IntToSex.getSex(model.data.myInfo.sex);
    int uid = model.data.myInfo.uid;
    String nickname = model.data.myInfo.nickname;
    String phone = model.data.myInfo.phone;
    String headerImage = model.data.myInfo.headimg;
    BindLover bindLover = IntToBindLover.getBindLover(model.data.bindLover);
    String formToken = await FormTokenApi().getFormToken(token);
    shareProvider.loginInit(token, userSex, LoginStatus.login, uid, formToken,
    bindLover, model.data.loverInfo, nickname, phone, headerImage);
    if(model.code == 200){
      return true;
    }return false;
  }
}
